<template>
  <div class="Rule-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          v-permissions="'rule-save'"
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
        <el-button
          v-permissions="'rule-multiDelete'"
          icon="el-icon-delete"
          type="danger"
          @click="handleMultiDelete($event)"
        >
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      row-key="id"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-permissions="'Rule-multiDelete'"
        align="center"
        fixed="left"
        show-overflow-tooltip
        type="selection"
      />
      <el-table-column
        align="left"
        label="名称"
        prop="title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="排序"
        prop="idx"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="状态"
        prop="status"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-tag v-if="scope.row.status" size="mini" type="success">
            启用
          </el-tag>
          <el-tag v-else size="mini" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="类型"
        prop="descri"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-tag
            v-if="hasRule(scope.row.rule_type, 'dir')"
            size="mini"
            type="primary"
          >
            目录
          </el-tag>
          <el-tag
            v-if="hasRule(scope.row.rule_type, 'menu')"
            size="mini"
            type="success"
          >
            菜单
          </el-tag>
          <el-tag
            v-if="hasRule(scope.row.rule_type, 'function')"
            size="mini"
            type="warning"
          >
            方法
          </el-tag>
          <el-tag
            v-if="hasRule(scope.row.rule_type, 'button')"
            size="mini"
            type="delete"
          >
            按钮
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="前端路由隐藏"
        prop="status"
        show-overflow-tooltip
      >
        <template #default="scope">
          <el-tag v-if="scope.row.hidden" size="mini" type="danger">是</el-tag>
          <el-tag v-else size="mini" type="success">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="创建时间"
        prop="created_at"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="240"
      >
        <template #default="{ row }">
          <el-link
            v-permissions="'rule-save'"
            class="mr12"
            icon="el-icon-plus"
            type="success"
            @click="handleAdd(row)"
          >
            添加子项
          </el-link>
          <el-link
            v-permissions="'rule-update'"
            class="mr12"
            icon="el-icon-edit"
            type="primary"
            @click="handleEdit(row)"
          >
            编辑
          </el-link>
          <el-link
            v-permissions="'rule-delete'"
            class="mr12"
            icon="el-icon-delete"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-link>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <edit ref="edit" :menus="menus" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, doMultiDelete, getTree } from '@/api/authRule'
  import Edit from './components/Edit'
  import { removeEmptyField } from '@/utils'

  export default {
    name: 'SystemRuleIndex',
    components: { Edit },
    data() {
      return {
        list: [],
        menus: [],
        listLoading: true,
        selectRows: '',
        queryForm: {
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleRule(row) {
        this.$refs['rule'].showRule(row)
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleAdd(row) {
        this.$refs['edit'].showEdit(row, 'addChild')
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗？', null, async () => {
          const { msg } = await doDelete(row.id)
          this.$baseMessage(msg, 'success', 'vab-hey-message-success')
          await this.fetchData()
        })
      },
      handleMultiDelete() {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id)
          console.log(ids)
          this.$baseConfirm('你确定要删除选中项吗？', null, async () => {
            const { msg } = await doMultiDelete(ids)
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
        }
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, menus },
        } = await getTree(removeEmptyField(this.queryForm))
        this.list = list
        this.menus = menus
        this.listLoading = false
      },
      hasRule(ruleTypes, target) {
        return ruleTypes.includes(target)
      },
    },
  }
</script>
